package edu.hawaii.duedates;

import java.util.Calendar;

/**
 * Book class used to store information on books borrowed.
 * 
 * @author John Zhou
 * @author John Ly
 */
public class BorrowedItem {
  private final String title;
  private final Calendar duedate;
  private final String type;
  private final String lender;
  
  /**
   * Constructor for BorrowedItem class.
   * @param title The title of the BorrowedItem.
   * @param duedate The due date of the BorrowedItem.
   * @param type The type of the BorrowedItem.
   * @param lender The lender of the BorrowedItem.
   */
  public BorrowedItem (String title, Calendar duedate, String type, String lender) {
    this.title = title;
    this.duedate = duedate;
    this.type = type;
    this.lender = lender;
  }
 
  /**
   * Prints out the BorrowedItem in a table-row form.
   * For example, "Jan 15, 2009 | Cat in the Hat | Book | Lender".
   * 
   * @return A String containing the book info.
   */
  public String toString() {
    int charsPerRow = 50;
    // "Due Date | Title | Lender | Book "
    String format = " %1$-14.14s | %2$-50.50s | %3$-10.10s | %4$-20.20s\n";
    int month = this.duedate.get(Calendar.MONTH) + 1;
    String date = "" + month;
    date = date + "-" + this.duedate.get(Calendar.DAY_OF_MONTH);
    date = date + "-" + this.duedate.get(Calendar.YEAR);
    if (this.title.length() <= charsPerRow) {
      return String.format(format, date, this.title, this.type, this.lender);
    }

    String output = String.format(format, date, this.title, this.type, this.lender);
    int rowsForTitle = this.title.length() / charsPerRow;
    if (this.title.length() % charsPerRow != 0) {
      rowsForTitle++;
    }
    for (int i = 1; i < rowsForTitle; i++) {
      String titleLong = this.title.substring(charsPerRow * i);
      output = output.concat(String.format(format, "", titleLong, "", ""));
    }
    return output;
  }  
  
  /**
   * Returns title of the book.
   * @return title The title of the book.
   */
  public String getTitle () {
    return this.title;
  }
  
  /**
   * Returns due date of the BorrowedItem.
   * @return The due date of the BorrowedItem.
   */
  public Calendar getDueDate () {
    return this.duedate;
  }
  
  /**
   * Returns the lender of the BorrowedItem.
   * @return  The lender of the BorrowedItem
   */  
  public String getLender () {
    return this.lender;
  }
  
  /**
   * Returns the type of the BorrowedItem.
   * @return The type of the BorrowedItem.
   */
  public String getType () {
    return this.type;
  }
}
